package com.cloud.hystrix.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.domain.TOrder;
import com.cloud.hystrix.service.IHystrixTOrderService;
import com.cloud.mapper.TOrderMapper;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/6/25 14:29
 * @Desc
 * @Remarks
 */
@Service
public class HystrixTOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements IHystrixTOrderService {

    @HystrixCommand(fallbackMethod = "serializable_TimeOutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "20000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
    })
    @Override
    public TOrder getById(Serializable id) {
        System.out.println("listAll");
        wait(5);
        return super.getById(id);
    }

    @HystrixCommand(fallbackMethod = "list_TimeOutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    @Override
    public List<TOrder> list() {
        System.out.println("listAll");
        wait(5);
        return super.list();
    }

    public TOrder serializable_TimeOutHandler(Serializable id) {
        System.out.println("服务降级方法！！！！！！");
        return null;
    }

    public List<TOrder> list_TimeOutHandler() {
        System.out.println("服务降级方法！！！！！！");
        return Collections.emptyList();
    }

    public String domain_TimeOutHandler(Long id) {
        System.out.println("服务降级方法！！！！！！");
        return "服务降级方法！！！！！！";
    }

    private void wait(int second) {
        try {
            System.out.println("执行休眠zzZZ");
            TimeUnit.SECONDS.sleep(second);
            System.out.println("休眠结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
        }
    }


    @HystrixCommand(fallbackMethod = "domain_TimeOutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
    })
    @Override
    public String findCircuitBreaker(Long id) {
        if (id < 0) {
            throw new RuntimeException("*****id不能是负数");
        }
        System.out.println("成功返回！");
        return "成功返回！";
    }

    public Object default_fallback(Object object) {
        System.out.println("全局返回");
        return "全局返回";
    }
}
